import React from 'react';
import { Redirect, Route, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';

export default function PrivateRoute({ component: Component, ...rest }) {
  const { isLogin } = useSelector(({ user }) => user);
  console.log('isLogin==>', isLogin);
  const location = useLocation();
  return (
    <Route
      {...rest}
      render={(props) =>
        isLogin ? (
          <Component {...props} />
        ) : (
          <Redirect to={{ pathname: '/login', state: { from: location.pathname } }} />
        )
      }
    />
  );
}
